package com.qianfeng.yime;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.yime.adapter.HotJobAdapter;
import com.qianfeng.yime.model.City;
import com.qianfeng.yime.model.HotJob;
import com.qianfeng.yime.model.Job;
import com.qianfeng.yime.util.CurrentSingleton;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Fcy on 2015/3/10.
 */

/**
 * 热门职位
 */
public class HotJobActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener<ListView> {
    public static final String URL = Constants.API_POINT_URL + "/v1/api/stu/job/hot/list?cityId=";
    private HttpUtils httpUtils;
    private int cityId = 34; // 默认为北京市
    private PullToRefreshListView refreshListView;
    private List<Job> list;
    private HotJobAdapter adapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_job);

        setTitle("热门职业");

        View emptyView = getLayoutInflater().inflate(R.layout.empty_list_layout, null);
        Button btnRetry = (Button) emptyView.findViewById(R.id.btn_loading_retry);
        btnRetry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData();
            }
        });

        refreshListView = (PullToRefreshListView) findViewById(R.id.list_hot_job);
        ListView listView = refreshListView.getRefreshableView();
        listView.setEmptyView(getLayoutInflater().inflate(R.layout.empty_list_layout, null));
        listView.setOnScrollListener(new PauseOnScrollListener(new BitmapUtils(this), false, true));
        list = new LinkedList<Job>();
        adapter = new HotJobAdapter(this, list);
        listView.setAdapter(adapter);

        // 从单例中获取当前选择的城市，如果没有，则默认为北京市34。

        //http://121.40.132.184/v1/api/stu/job/hot/list?cityId=37
        httpUtils = new HttpUtils(3000);

        // 获取当前选择的城市的id
        CurrentSingleton currents = CurrentSingleton.getInstance();
        City currentCity = currents.getCurrentCity();
//        if (currentCity == null) { // 当前未选择城市
//            cityId = 34;
//        } else {
//            cityId = currentCity.getId();
//        }
        if (currentCity != null) {
            cityId = currentCity.getId();
        }
        // 进入后加载数据
        loadData();

        refreshListView.setOnRefreshListener(this);
    }

    /**
     * 加载数据
     */
    public void loadData() {
        httpUtils.send(HttpRequest.HttpMethod.GET, URL + cityId, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                // 关闭下拉刷新
                refreshListView.onRefreshComplete();
                String result = responseInfo.result;
                try {
                    JSONObject json = new JSONObject(result);
                    int code = json.getInt("code");
                    if (code == 200) {
                        JSONArray data = json.getJSONArray("data");
                        // !!! 这样做可以处理新添加的数据显示在前面，并且顺序不变
                        for (int i = data.length() - 1; i >= 0; i--) {
                            JSONObject jsonObject = data.getJSONObject(i);
                            Job job = new Job();
                            job.parseJson(jsonObject);
                            if (!list.contains(job)) {
                                list.add(0, job);
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                refreshListView.onRefreshComplete();
                Log.i(">>", "失败:" + s);
            }

        });
    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        loadData();
    }
}